package com.sma.web.config;


import java.io.IOException;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.FileSystemResource;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.multipart.support.StandardServletMultipartResolver;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

/**** imports ****/
@Configuration
//定义扫描的包，加载控制器
@ComponentScan("com.sma.controller")
//驱动Spring Web MVC
@EnableWebMvc
public class WebConfig  {

    /***
     * 创建视图解析器
     *
     * @return 视图解析器
     */
    @Bean(name = "viewResolver")
    public ViewResolver initViewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/WEB-INF/jsp/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }

    @Bean(name = "multipartResolver")
    public MultipartResolver initMultipartResolver() {
        return new StandardServletMultipartResolver();
    }

//	@Bean(name = "multipartResolver")
//	public MultipartResolver initCommonsMultipartResolver() {
//		// 文件上传路径
//		String filepath = "e:/mvc/uploads";
//		// 5MB
//		Long singleMax = (long) (5 * Math.pow(2, 20));
//		// 10MB
//		Long totalMax = (long) (10 * Math.pow(2, 20));
//		CommonsMultipartResolver multipartResolver
//		        = new CommonsMultipartResolver();
//		multipartResolver.setMaxUploadSizePerFile(singleMax);
//		multipartResolver.setMaxUploadSize(totalMax);
//		try {
//			// 设置保存路径
//			multipartResolver.setUploadTempDir(new FileSystemResource(filepath));
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
//		return multipartResolver;
//	}
}
